<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var maxScore = function (cardPoints, k) {
      const n = cardPoints.length;
      // 滑动窗口大小为 n-k
      const windowSize = n - k;
      // 选前 n-k 个作为初始值
      let sum = 0;
      for (let i = 0; i < windowSize; ++i) {
        sum += cardPoints[i];
      }
      let minSum = sum;
      for (let i = windowSize; i < n; ++i) {
        // 滑动窗口每向右移动一格，增加从右侧进入窗口的元素值，并减少从左侧离开窗口的元素值
        sum += cardPoints[i] - cardPoints[i - windowSize];
        minSum = Math.min(minSum, sum);
      }
      let totalSum = 0;
      for (let i = 0; i < n; i++) {
        totalSum += cardPoints[i];
      }
      return totalSum - minSum;
    };
    console.log(maxScore(cardPoints = [1, 2, 3, 4, 5, 6, 1], k = 3))
  </script>
</body>

</html>